﻿using Core.Configuration.Api;
using Core.Configuration.Helper;
using IconYuan.Api.Services;
using IconYuan.Manager.Markets;
using IconYuan.Manager.Orders;
using IconYuan.Manager.Systems;
using IconYuan.Manager.Users;
using IconYuan.Model.Entity.Markets;
using IconYuan.Model.Entity.Orders;
using IconYuan.Model.Entity.Products;
using IconYuan.Model.Entity.Systems;
using IconYuan.Model.Entity.Users;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace IconYuan.Api.Controllers.Systems
{
    /// <summary>
    /// 系统操作
    /// </summary>
    public class SystemController : ApiBase
    {
        readonly FeedBackManager feedSvc;
        readonly UserGradeManager gradeSvc;
        readonly UserManager userSvc;
        readonly OrderManager orderSvc;
        readonly OrderGoodsManager orderGoodsSvc;
        readonly UserCouponManager userCouponSvc;
        public SystemController()
        {
            feedSvc = new FeedBackManager();
            gradeSvc = new UserGradeManager();
            userSvc = new UserManager();
            orderSvc = new OrderManager();
            orderGoodsSvc = new OrderGoodsManager();
            userCouponSvc = new UserCouponManager();
        }

        #region 留言反馈

        /// <summary>
        /// 添加意见反馈
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        [HttpPost]
        public async Task<bool> AddFeedBack(FeedBack model)
        {
            if (model.user_id <= 0) throw new Exception("缺少用户ID参数");
            if (string.IsNullOrEmpty(model.content)) throw new Exception("请填写详细内容");
            model.create_time = DateTime.Now;
            model.store_id = GetStoreId();
            var id = await feedSvc.InsertAsync(model);
            return id > 0;
        }
        #endregion

        #region 定时任务    
        
        /// <summary>
        /// 清理过期VIP
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<bool> ResetVIP()
        {
            var defGrade = DtoHelper.GetGradeList().Find(x => x.is_default == 1);
            var result = await userSvc.UpdateAsync(x => new User() { grade_id = defGrade.grade_id, expire_time = Convert.ToDateTime("2030-01-01") },
                  y => y.grade_id != defGrade.grade_id && y.expire_time < DateTime.Now);
            return result;
        }

        /// <summary>
        /// 清流过期优惠券
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<bool> ClearCoupon()
        {
            var date = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
            var couponList = await userCouponSvc.GetListAsync(x => x.is_use == 0 && x.is_expire == 0 && x.end_time < date, y => y.user_coupon_id, OrderByType.Asc);
            var ids = couponList.Select(x => x.user_coupon_id).ToList();

            await userCouponSvc.UpdateAsync(x => new UserCoupon { is_expire = 1 }, y => y.is_use == 0 && y.end_time < date && ids.Contains(y.user_coupon_id));
            return true;
        }

        /// <summary>
        /// 清理无效订单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<bool> ClearOrder()
        {
            var statusList = new List<int>() { 10, 20 };
            var orderList = await orderSvc.GetListAsync(x => statusList.Contains(x.order_status));
            var result = await orderSvc.DeleteAsync(x => statusList.Contains(x.order_status));
            var orderIds = orderList.Select(x => x.order_id);
            //删订单商品
            await orderGoodsSvc.DeleteAsync(x => x.order_id > 0 && orderIds.Contains(x.order_id));
            return result;
        }

        /// <summary>
        /// 定时任务测试
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string TestJob()
        {
            return "success";
        }
        #endregion

        #region 公众号推送 
        /// <summary>
        /// 公众号事件通知
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ContentResult MpNotity()
        {

            return XmlSuccess();
        }
        private ContentResult XmlSuccess()
        {
            var formBody = GetBody();
            LogHelper.WriteLog("mp", formBody);
            return new ContentResult
            {
                Content = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>",
                ContentType = "application/xml",
                StatusCode = 200
            };
        }
     
        #endregion
    }
}